#include"faacapi.h"
static const uint32_t GF_M4ASampleRates[] ={
	96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
	16000, 12000, 11025, 8000, 7350, 0, 0, 0
};
uint32_t FAACGetSampleRatesByIndex(int nIndex){
   return GF_M4ASampleRates[nIndex];
}
#define ADSTHEADLEN 7
//读取信息
int FAACGetAudioSpecificConfig(const char*filename,FILE_FAAC*fileinfo){
	uint8_t ADST[ADSTHEADLEN]={0};
	ADSTHEADINFO Info={0};
	int pos=0;
	fileinfo->fp=fopen(filename,"rb");
	if(fileinfo->fp==NULL)
		return -1;
	pos=ftell(fileinfo->fp);
	if(fread(ADST,1,ADSTHEADLEN,fileinfo->fp)!=ADSTHEADLEN)
		return -1;
	if((ADST[0]!=0xFF)&&((ADST[1]&0xF0)!=0xF0)){
		printf("0xFFF0错误\n");
		return -1;
	}
	Info.is_mp2=(ADST[1]&0x08)>>3;
	Info.profile=(ADST[2]&0xC0)>>6;
	Info.sr_idx=(ADST[2]&0x3C)>>2;
	Info.nb_ch=((ADST[2]&0x01)<<2)|((ADST[3]&0xC0)>>6);
	Info.frame_size=((ADST[3]&0x03)<<11)|((ADST[4]&0xFF)<<3)|((ADST[5]&0xE0)>>5);
	
	fileinfo->frameinfo=Info;

	fseek(fileinfo->fp,pos,SEEK_SET);	
	return 0;
}
//获取下一帧长度并定位到数据开始位置
uint32_t FAACGetNextFrameSize(FILE_FAAC*fileinfo){
	uint8_t ADST[ADSTHEADLEN]={0};
	ADSTHEADINFO Info={0};
	int pos=ftell(fileinfo->fp);
	if(fread(ADST,1,ADSTHEADLEN,fileinfo->fp)!=ADSTHEADLEN)
	   return 0;
	if(ADST[0]!=0xFF&&(ADST[1]&0xF0!=0xF0)){
	   printf("0xFFF0错误\n");
	   return 0;
	}
	Info.is_mp2=(ADST[1]&0x08)>>3;
	Info.profile=(ADST[2]&0xC0)>>6;
	Info.sr_idx=(ADST[2]&0x3C)>>2;
	Info.nb_ch=((ADST[2]&0x01)<<2)|((ADST[3]&0xC0)>>6);
	Info.frame_size=((ADST[3]&0x03)<<11)|((ADST[4]&0xFF)<<3)|((ADST[5]&0xE0)>>5);
//	printf("is_mp2:%hhu profile:%hhu sr_idx:%hhu nb_ch:%hhu frame_size:%hu\n",
//			Info.is_mp2,Info.profile,Info.sr_idx,Info.nb_ch,Info.frame_size);
	fseek(fileinfo->fp,pos,SEEK_SET);
	return Info.frame_size;	
}
//读取数据
void FAACGetFrameBuff(FILE_FAAC*fileinfo,uint8_t*buff,uint32_t buffsize){
	 fread(buff,1,buffsize,fileinfo->fp);
}